package com.pinda.controller;

import com.github.pagehelper.PageInfo;
import com.pinda.auth.entity.PdAuthRole;
import com.pinda.auth.entity.PdAuthRole;
import com.pinda.auth.search.PdAuthRoleSearch;
import com.pinda.auth.view.AuthorizationView;
import com.pinda.auth.view.ConfigView;
import com.pinda.commonutils.Page;
import com.pinda.commonutils.Result;
import com.pinda.commonutils.StatusCode;

import com.pinda.service.IPdAuthRoleService;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author 黄杰
 * @since 2023-06-13
 */
@RestController
@RequestMapping("/pd_auth_role")
public class PdAuthRoleController {
    @Resource
    private IPdAuthRoleService iPdAuthRoleService;

    /**
     * TODO 分页条件查询所有角色
     * @param page
     * @return Result<PageInfo<PdAuthUser>>
     */
    @RequestMapping("findRole")
    public Result<PageInfo<PdAuthRole>> findRole(@RequestBody Page<PdAuthRoleSearch> page){
        return new Result<PageInfo<PdAuthRole>>(true, StatusCode.OK, "查询成功",this.iPdAuthRoleService.findRoleService(page));
    }

    /**
     * TODO 新增角色
     * @param pdAuthRole
     * @return Result<Boolean>
     */
    @RequestMapping("addRole")
    public Result<Boolean> addRole(@RequestBody PdAuthRole pdAuthRole){
        return new Result<Boolean>(true, StatusCode.OK, "添加成功",this.iPdAuthRoleService.addRoleService(pdAuthRole));
    }

    /**
     * TODO 根据角色id删除角色信息
     * @param id
     * @return Result<Boolean>
     */
    @RequestMapping("removeRole/{id}")
    public Result<Boolean> removeRole(@PathVariable("id") Long id){
        return new Result<Boolean>(true, StatusCode.OK, "删除成功",this.iPdAuthRoleService.removeById(id));
    }
    /**
     * TODO 根据角色id修改角色信息
     * @param pdAuthRole
     * @return Result<Boolean>
     */
    @RequestMapping("updateRole")
    public Result<Boolean> updateRole(@RequestBody PdAuthRole pdAuthRole){
        return new Result<Boolean>(true, StatusCode.OK, "编辑成功",this.iPdAuthRoleService.updateById(pdAuthRole));
    }

    /**
     * TODO 查询不是自己的用户具有的角色 授权
     * @param roleId
     * @param userId
     * @return
     */
    @RequestMapping("findAuthorization/{roleId}/{userId}")
    public Result<AuthorizationView> findAuthorization(@PathVariable("roleId") String roleId, @PathVariable("userId") String userId){
        return new Result<AuthorizationView>(true,StatusCode.OK,"查询成功",this.iPdAuthRoleService.findAuthorizationService(roleId, userId));
    }

    /**
     *  TODO 授权或回收用户角色
     * @param authorizationView
     * @return Result<Boolean>
     */
    @RequestMapping("batchAuthorization")
    public Result<Boolean> batchAuthorization(@RequestBody AuthorizationView authorizationView){
        return new Result<Boolean>(true,StatusCode.OK,"授权成功",this.iPdAuthRoleService.batchAuthorizationService(authorizationView));
    }

    /**
     * TODO 根据角色id查询具有的菜单和资源
     * @param roleId
     * @return Result<ConfigView>
     */
    @RequestMapping("findConfig/{roleId}")
    public Result<ConfigView> findConfig(@PathVariable("roleId") String roleId){
        System.out.println(">>>>>>>>>>"+roleId);
        return new Result<ConfigView>(true,StatusCode.OK,"查询成功",this.iPdAuthRoleService.findConfigService(roleId));
    }
    @RequestMapping("batchConfig")
    public Result<Boolean> batchConfig(@RequestBody ConfigView configView){
        return new Result<Boolean>(true,StatusCode.OK,"配置成功",this.iPdAuthRoleService.batchConfigService(configView));
    }
}
